//
//  SQEmergencyPlanViewController.m
//  energy
//
//  Created by 苏青 on 2021/9/30.
//

#import "SQEmergencyPlanViewController.h"
#import "WorkDetailViewController.h"
#import "SVProgressHUD.h"
@interface SQEmergencyPlanViewController ()<BaseTableVCInterface>

@end

@implementation SQEmergencyPlanViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"应急预案";
    
    LC_WEAK_SELF
    self.cellEvent = ^(int key) {
        SQPlanModelFiles * dataModel = weakSelf.planModelData.files[key];
        [SVProgressHUD show];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 获取Caches目录
                        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

                        NSString * filePath = [cachesPath stringByAppendingFormat:@"/%@",dataModel.epd_name];
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        if ([fileManager fileExistsAtPath:filePath]) {

                        }else{
                        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_FilesHeader,dataModel.epd_enclosure]];
                        NSData *data = [NSData dataWithContentsOfURL:url];
                        NSString * filePath = filePath = [weakSelf addFile:dataModel.epd_name contents:data];
                        }
            NSLog(@"%@",filePath);
            if (filePath.length != 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    WorkDetailViewController *vc = WorkDetailViewController.new;
                    vc.filePath = filePath;
                    vc.n_title = dataModel.epd_grade_text;
                    [weakSelf pushVC:vc];
                });
            }
            
        });
    };
}


-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.planModelData.files[indexPath.row];
}



-(NSInteger)rowsCount:(NSInteger)section{
    return self.planModelData.files.count;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQLookPlanTableViewCell"]];
}

- (NSString *)addFile:(NSString *)filePath contents:(NSData *)data{
    // 获取Caches目录
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    filePath = [cachesPath stringByAppendingFormat:@"/%@",filePath];
    
    if (filePath.length==0) {
        return @"";
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        return filePath;
    }
    NSError *error;
    NSString *dirPath = [filePath stringByDeletingLastPathComponent];
    BOOL isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
    if (error) {
        NSLog(@"creat File Failed:%@",[error localizedDescription]);
    }
    if (!isSuccess) {
        return @"";
    }
    isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    
    [data writeToFile:filePath atomically:YES];
    return filePath;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
